package cn.kgc.ssm.service.impl;

import cn.hutool.crypto.SecureUtil;
import cn.kgc.ssm.dto.UserPageDTO;
import cn.kgc.ssm.entity.User;
import cn.kgc.ssm.mapper.UserMapper;
import cn.kgc.ssm.service.UserService;
import cn.kgc.ssm.util.ResponseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: mosin
 * @version: v1.0  2022/12/14
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public ResponseResult getPage(UserPageDTO userPageDTO) {

        Integer page = userPageDTO.getPage();
        Integer limit = userPageDTO.getLimit();
        String username = userPageDTO.getUsername();

        //开启分页
        PageHelper.startPage(page, limit);
        List<User> users = userMapper.selectAll(username);
        PageInfo<User> userPageInfo = new PageInfo<>(users);

        //获取总条数
        long total = userPageInfo.getTotal();
        // 获取当前页数据
        List<User> list = userPageInfo.getList();

        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setMsg("数据查询成功");
        responseResult.setCount(total);
        responseResult.setData(list);

        return responseResult;
    }

    @Override
    public ResponseResult addUser(User user) {

        ResponseResult responseResult = new ResponseResult();
        List<User> users = userMapper.selectAll(user.getUsername());

        if(users.size()>0){
            responseResult.setMsg("用户名已存在");
            responseResult.setCode(500);
            return responseResult;
        }

        userMapper.insertUser(user);

        responseResult.setMsg("添加成功");
        responseResult.setCode(0);
        return responseResult;
    }

    @Override
    public User getById(Integer userId) {
        User user =  userMapper.selectById(userId);
        return user;
    }

    @Override
    public ResponseResult editUser(User user) {

        ResponseResult responseResult = new ResponseResult();
        userMapper.updateUser(user);
        responseResult.setCode(0);
        responseResult.setMsg("更新成功");
        return responseResult;
    }

    @Override
    public ResponseResult updateStatus(Integer userId, Integer status) {

        userMapper.updateStatus(userId,status);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setMsg("状态更新成功");
        responseResult.setCode(0);
        return responseResult;
    }

    @Override
    public ResponseResult deleteByIds(String ids) {

        String[] split = ids.split(",");
        List<String> strings = Arrays.asList(split);

        List<Integer> intIds = strings.stream().map(id -> {
            return Integer.valueOf(id);
        }).collect(Collectors.toList());

        userMapper.deleteById(intIds);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setMsg("删除成功");
        return responseResult;
    }

    @Override
    public ResponseResult login(String username, String password) {
        ResponseResult responseResult = new ResponseResult();

        List<User> users = userMapper.selectAll(username);
        if(users.size()>0){
            User user = users.get(0);
            String md5pwd = user.getPassword();
            if(md5pwd.equals(SecureUtil.md5(password))){
                if(user.getStatus()==0){
                    responseResult.setCode(500);
                    responseResult.setMsg("账号被锁定，请联系管理员");
                }else{
                    responseResult.setCode(0);
                    responseResult.setMsg("登录成功");
                }
            }else{
                responseResult.setCode(500);
                responseResult.setMsg("密码不正确");
            }

        }else{
            responseResult.setCode(500);
            responseResult.setMsg("用户名不存在");
        }
        return responseResult;
    }


}
